﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace backend_wcf
{
    public class User
    {
        public enum Type
        {
            guest,
            normal,
            admin
        }
        string userName;
        string passWord;
        Database db;

        private static string hashPwd(string pwd)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider hash = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] raw = System.Text.Encoding.ASCII.GetBytes(pwd);
            raw = hash.ComputeHash(raw);
            string ret = "";
            foreach (byte b in raw)
                ret += b.ToString("x2").ToLower();
            return ret;
        }
        public User(string userName, string passWord, Database db)
        {
            this.userName = userName;
            this.passWord = passWord;
            this.db = db;
        }
        public bool addUser(User.Type usertype)
        {
            try
            {
                string passwd_md5 = hashPwd(passWord);
                Dictionary<String, object> d = new Dictionary<string, object>();
                d.Add("username", userName);
                d.Add("passwd_md5", passwd_md5);
                d.Add("usertype", usertype);
                db.addData("users", d);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public User.Type checkUser()
        {
            System.Data.DataSet ds = null;
            //try
            //{
                ds = db.getData("users", "username='" + userName+ "'");
                foreach (System.Data.DataRow row in ds.Tables["users"].Rows)
                {
                    if (row["passwd_md5"].ToString() == hashPwd(passWord))
                    {
                        ds = null;
                        //return (User.Type)((int)row["usertype"]);
                        //return User.Type.admin;
                        //int usertype = Convert.ToInt32(row["usertype"].ToString());
                        int usertype = (int)row["usertype"];
                        if (usertype == 1)
                        {
                            return User.Type.normal;
                        }
                        else if (usertype == 2)
                        {
                            return User.Type.admin;
                        }
                        else
                        {
                            return User.Type.guest;
                        }
                    }
                }
                ds = null;
                //return User.Type.normal;
                return User.Type.guest;
            /*}
            catch (Exception e)
            {
                ds = null;
                return User.Type.guest;
            }*/
        }

    }
}
